package com.example.resourceSharingPlatform.dao;


import com.example.resourceSharingPlatform.entity.User;
import com.example.resourceSharingPlatform.entity.User_trans;
import com.example.resourceSharingPlatform.mappers.UserTransRowMapper;
import com.example.resourceSharingPlatform.security_entity.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class User_transDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 根据userId查询交易流水
     * */
    public List<User_trans> queryTransByUserID(String id){
        String sql = "SELECT * FROM user_trans WHERE user_id=?";
        List<User_trans> list = jdbcTemplate.query(sql,new UserTransRowMapper(),id);
        return list;
    }

    /**
     * 根据交易流水的id删除
     * */
    public boolean deleteTransById(int id){
        String sql = "DELETE FROM user_trans WHERE id=?";
        int row = jdbcTemplate.update(sql, id);
        return row != 0;
    }

    /**
     * 插入一条新的交易流水
     * */
    public User_trans insertTrans(User_trans user_trans){
        String sql = "INSERT INTO user_trans VALUES(?,?,?,？)";
        jdbcTemplate.update(sql, null, user_trans.getUser_id(), user_trans.getType(),user_trans.getTime());
        String sql2 = "SELECT FROM users_trans WHERE id= (SELECT MAX(id) FROM user_trans)";
        return jdbcTemplate.queryForObject(sql2, new UserTransRowMapper());
    }
}
